<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\View;
use think\Model;
use app\index\model\DiaoduModel;
use app\index\model\IndexModel;
class Diaodu extends Controller{
	public function index(){
		$view=new View();
		return $view->fetch();
	}
	//订单调度
public function sendorder(){
	if($_POST['order_id']){
		$order_id=$_POST['order_id'];
		$dname_en=$_POST['dname'];
		$dname=$_POST[$dname_en];
		$ps_date= $_POST['ps_date'];
		$ps_time = $_POST['ps_time'];
		$date=strtotime(date("Y-m-d"));
		if ($ps_date == 1) {
			$date = strtotime(date("Y-m-d"));
		} elseif ($ps_date == 2) {
			$date = strtotime(date("Y-m-d", strtotime("+1 day")));
		} elseif ($ps_date == 3) {
			$date = strtotime(date("Y-m-d", strtotime("+2 day")));
		}
	
		$data1=[
			'ps_date'=>$date,
			'ps_time'=>$ps_time
		];	
		Db::name("waimai_order")->where(array("order_id"=>$order_id))->update($data1);
		$order=Db::name("waimai_order")->where(array("order_id"=>$order_id))->find();
						if($ps_time == 9){
							$order['time']='9:00-10:00';
						}elseif($ps_time == 10){
							$order['time']='10:00-11:00';
						}elseif($ps_time == 11){
							$order['time']='11:00-12:00';
						}elseif($ps_time == 15){
							$order['time']='15:00-16:00';
						}elseif($ps_time == 16){
							$order['time']='16:00-17:00';
						}elseif($ps_time == 17){
							$order['time']='17:00-18:00';
						}
	//获取每个订单的菜单名称和数量 'status'=>[['EQ',NULL],['=',1],'or']
	$m=new IndexModel;
	$m->setSellStatus($order_id);
		$goods=$m->getGoods($order_id);	
		$address=Db::name("waimai_user_address")->where(array("address_id"=>$order['address_id']))->find();
		
		
		if($order['pay_type']=="offline"){
			$order['pay_type']="线下支付 ";
		}else{
			$order['pay_type']="微信支付 ";
		}
		if($order['is_mobile_pay']==0){
			$order['is_mobile_pay']='未支付';
		}else{
			$order['is_mobile_pay']='已支付';
		}
		//优惠
		$discount_detail=json_decode($order['discount_detail'],TRUE);
		
			$format_str ="<center>@@2 ".$dname."</center>";
		
		$format_str .= chr(10) ."订单号：" . $order['order_id'];
		$format_str .= chr(10) ."配送日期：" . date("Y-m-d",$order['ps_date']);
		$format_str .= chr(10) ."配送时间：" . $order['time'];
		$format_str .= chr(10) ."联系人：" . $address['name'];
		$format_str .= chr(10) . "联系电话：" . $address['phone'];
		$format_str .= chr(10) . "送货地址：" . $address['address'].$address['detail'];
		$format_str .= chr(10) . "支付方式：" . $order['pay_type'];
		$format_str .= chr(10) . "支付状态：".$order['is_mobile_pay'];
		$format_str .= chr(10) . "下单时间：".date("Y-m-d H:i",$order['create_time']);
		$format_str .= chr(10) . "备注：" . $order['desc'];
		$format_str .= chr(10) . "************************";
		$format_str .= chr(10) . "菜品    数量/规格   金额  ";//won添加打印内容
		foreach($goods as $val){
			$format_str .= chr(10) . $val['name'] . "：" . $val['num'] . "/" . $val['guige'] . "\t*\t" . $val['allprice']  ;//won添加unit量
			//$qr.=$val['name'].':'.$val['num'].$val['guige'].'/'.$val['allprice'].'||';
		}
		$format_str .= chr(10) . "************************";
		if(!empty($discount_detail[0])){
			$format_str .= chr(10) . "优惠活动: " . $discount_detail[0]['desc'];
		}
		$format_str .= chr(10) . "总价: ￥" . $order['price'];
		$format_str .= chr(10) . "实际支付：￥" . $order['discount_price'];
		$format_str .= chr(10) . "打单时间：" . date("Y-m-d H:i:s");
//			$format_str .='<q>'.$qr.'</q>';
//			$format_str .='<q>http://weixin.qq.com/r/Y0RSSlLEng-hrRYp9xFx</q>';
//			dump($discount_detail);exit;
		//发送打印
		$diaodu_model=new DiaoduModel;
		$sest=$diaodu_model->send($format_str,$dname_en);
		$dtime=time();
		$dataupdate=[
			'order_status'=>9	//9表示已调度
		];
		Db::name("waimai_order")->where(array("order_id"=>$order_id))->update($dataupdate);
		//检查是否调度过
		$dorder=Db::name('waimai_diaodu')->where(array("order_id"=>$order_id))->find();
		if(!empty($dorder)){
			$ddata=[
				'dname_en'=>$dname_en,
				'dname'=>$dname,
				'dtime'=>$dtime,
				'status'=>1
			];
			Db::name("waimai_diaodu")->where(array("order_id"=>$order_id))->update($ddata);
		}else{
			//如果没调度过就插入一条新数据
			$store_id=$order['store_id'];
			$ddata=[
			'store_id'=>$store_id,
			'order_id'=>$order_id,
			'dname_en'=>$dname_en,
			'dname'=>$dname,
			'dtime'=>$dtime,
			'status'=>1
			];
			Db::name('waimai_diaodu')->insert($ddata);
		}
		if(!empty($_POST['pshas'])){
			$m->cgdelete($order_id);
		}
		$this->success("订单调度成功！该订单在‘所有订单’列表里可以看到");
		
	}

}
public function huidiao($order_id){
	$data=[
		'order_status'=>2
	];
	Db::name("waimai_order")->where(array("order_id"=>$order_id))->update($data);
	$ddata=[
		'status'=>0
	];
	Db::name("waimai_diaodu")->where(array("order_id"=>$order_id))->update($ddata);
	$this->redirect("Index/neworder_list");
}


}
?>